package com.smforj.ssm.frame.core.aop; 

import org.apache.ibatis.logging.Log;
import org.apache.ibatis.logging.LogFactory;
import org.aspectj.lang.JoinPoint;

import com.smforj.ssm.frame.core.dao.DbContextHolder;
import com.smforj.ssm.frame.core.enums.JDataSource;
import com.smforj.ssm.frame.core.utils.StringEx;
import com.smforj.ssm.util.PropKit;

/**
 * 数据库源拦截器,拦截Dao层对象
 * 
 * 
 * @author Haijun Gao 
 * @date 2016-8-5 下午5:45:01
 * @desp http://www.cnblogs.com/saga5998/
 * @email 573107773@qq.com
 */ 
public class DataSourceAop {
	private static final Log logger = LogFactory.getLog("DataSourceAop"); 
	 
	public void setDataSource(JoinPoint jp) { 
		
		//拦截的实体类   
		Object target = jp.getTarget();  
		//拦截的方法名称   
		String methodName = jp.getSignature().getName();  
        if(logger.isDebugEnabled()) 
        {
        	logger.debug(target.getClass().getName()); 
        	logger.debug(methodName);
	        logger.debug("DataSourceAop......"+DbContextHolder.getCustomerType());   
        }
        JDataSource ds = target.getClass().getName().contains("com.sun.proxy") ? 
        		jp.getTarget().getClass().getSuperclass().getAnnotation(JDataSource.class) 
        		: jp.getTarget().getClass().getAnnotation(JDataSource.class); 
        if(ds==null || StringEx.isExEmpty(ds.value()))
        { 
	        if(logger.isDebugEnabled()) 
	        	logger.debug("ds==null setDataSource......" + PropKit.get("defaultData")); 
	    	DbContextHolder.setCustomerType(PropKit.get("defaultData")); 
        }
        else
        {
        	DbContextHolder.setCustomerType(ds.value());  
            if(logger.isDebugEnabled())
            { 
            	logger.debug("setDataSource......"+ds.value());  
    	        logger.debug("DataSourceAop......"+DbContextHolder.getCustomerType());  
            } 
        }
	}  

}
